package com.zygh.lz.service.impl;

import com.zygh.lz.dao.*;
import com.zygh.lz.entity.Patrolrecord;
import com.zygh.lz.entity.PoliceData;
import com.zygh.lz.service.RecordService;
import com.zygh.lz.stapprove.ResultUtil;
import com.zygh.lz.util.DataTime;
import com.zygh.lz.vo.ResultBean;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class RecordServiceImpl implements RecordService {

    @Resource
    private GpsMapper gpsMapper;
    @Resource
    private SignServiceImpl signService;
    @Resource
    private SignMapper signMapper;
    @Resource
    private PatrolrecordMapper patrolrecordMapper;
    @Resource
    private BrigadeRecordMapper brigadeRecordMapper;



    private static DecimalFormat df = new DecimalFormat("#");

    @Override
    public ResultBean calculateVitality(Integer staffId, Integer sectionId, String startTime, String endTime) {
        //时长（单位：小时）
        int hour = 0;
        //里程（单位：公里）
        int mileages = 0;
        //（活跃度=里程/时长）计算个人活跃度
        if (staffId != null) {
            Map duration = signService.duration(staffId, startTime, endTime);
            hour = Integer.valueOf(duration.get("hour").toString());
            mileages = Integer.valueOf(duration.get("kilometre").toString());
        }
        //大队活跃度
        if (staffId == null && sectionId != null) {
            Map durations = durations(sectionId, startTime, endTime);
            hour = Integer.valueOf(durations.get("hour").toString());
            mileages = Integer.valueOf(durations.get("mileages").toString());
        }
        System.out.println("大队：" + sectionId);
        System.out.println("人员：" + staffId);
        System.out.println("里程：" + mileages);
        System.out.println("时长：" + hour);
        String format;
        if (mileages == 0 || hour == 0) {
            format = "0";

        } else {
            format = df.format(mileages / hour);

        }
        return ResultUtil.setOK("success", format);
    }

    //计算大队时长和巡查里程
    public Map durations(Integer sectionId, String startDate, String endDate) {
        List<Patrolrecord> patrolrecords = patrolrecordMapper.findRecord(sectionId, startDate, endDate);
        //巡查分钟
        int duration = 0;
        double mileages = 0;
        long dura = 0;
        for (Patrolrecord patrolrecord : patrolrecords) {
            //Date date = new Date();
            //上班时长
            if (patrolrecord.getPatrolRecordEndtime() == null) {
                //dura = DataTime.getDateduration(date, patrolrecord.getPatrolRecordBegintime());
                dura = 0;
            } else {
                dura = DataTime.getDateduration(patrolrecord.getPatrolRecordEndtime(), patrolrecord.getPatrolRecordBegintime());
            }
            duration += dura;
            mileages += gpsMapper.findMileages(patrolrecord.getSignnum());
        }
        //分钟转小时
        System.out.println("duration:" + duration);
        long hour = duration / 60;
        Map map = new HashMap();
        map.put("hour", hour);
        map.put("mileages", df.format(mileages / 1000));
        return map;
    }


    @Override
    public ResultBean gateStreet(Integer staffId, String startTime, String endTime) {
        List<Map> mapList = gpsMapper.gateStreet(staffId, startTime, endTime);

        return ResultUtil.setOK("success", mapList);
    }

    /**
     * 查询签到人数及活跃度
     *
     * @param sectionId 部门id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 签到人数及活跃度
     */
    @Override
    public ResultBean calculateVitalityPlus(Integer sectionId, String startTime, String endTime) {
        if (sectionId == null) {
            List<Map<String, String>> maps = signMapper.countSignedOfDSectionAndStaffType(startTime, endTime);
            List<Map<String, String>> activity =
                    brigadeRecordMapper.getActivityOfDSection(startTime, endTime);

            List<Map> collect = maps.stream().peek(x -> activity.stream()
                    .filter(y -> Objects.equals(y.get("sectionId"), x.get("sectionId")))
                    .forEach(z -> x.put("rate", z.get("rate")))).collect(Collectors.toList());

            return ResultUtil.setOK("success", collect);
        } else {
            List<Map<String, String>> maps = signMapper.countSignedOfZSectionAndStaffType(sectionId, startTime, endTime);
            List<Map<String, String>> activity =
                    brigadeRecordMapper.getActivityOfZSection(sectionId, startTime, endTime);

            List<Map> collect = maps.stream().peek(x -> activity.stream()
                    .filter(y -> Objects.equals(y.get("sectionId"), x.get("sectionId")))
                    .forEach(z -> x.put("rate", z.get("rate"))))
                    .collect(Collectors.toList());

            return ResultUtil.setOK("success", collect);
        }

    }

    @Override
    public Map findJieChuJingByCode(int id, String startTime, String endTime) {
        if (StringUtils.isEmpty(startTime) && StringUtils.isEmpty(endTime)) {
            //默认七天
            startTime = DataTime.getStartDate();
            endTime = DataTime.getEndDate();
        }



        return null;
    }
}
